<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractEditAction.class.php';

/**
 * Mileage_AcceptAction
**/
class Mileage_AcceptAction extends Mileage_AbstractEditAction
{
	/**
	 * _getId
	 * 
	 * @param   void
	 * 
	 * @return  int
	**/
	protected function _getId()
	{
		return $this->mRoot->mContext->mRequest->getRequest('rec_id');
	}

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'rec');
		return $handler;
	}

	/**
	* _setupActionForm
	* 
	* @param   void
	* 
	* @return  void
	**/
	protected function _setupActionForm()
	{
		$this->mActionForm =& $this->mAsset->getObject('form', 'rec', FALSE, 'edit');// isAdmin:FALSE
		$this->mActionForm->prepare();
	}

	/**
	 * hasPermission
	 * 
	 * @param   void
	 * 
	 * @return  bool
	**/
	public function hasPermission()
	{
		$root =& XCube_Root::getSingleton();
		$result = FALSE;

		// check user
		if (Mileage_Utils::getModuleConfig('use_remittance', $this->mAsset->mDirname) && is_object($root->mContext->mXoopsUser)) {
			$mile_handler = $this->mAsset->getObject('handler', 'mile');
			$mile_id = intval($this->mRoot->mContext->mRequest->getRequest('mile_id'));
			$mile =& $mile_handler->getObjects(new Criteria('mile_id', $mile_id));
			$mile_url = $mile[0]->get('url');
			if (!empty($mile_url)) {
				// check ticket's permission
				if (strpos($mile_url, 'remittance') === 0 && $receive_uid = intval(substr(strrchr($mile_url, ':'), 1))) {
					$my_uid = intval($root->mContext->mXoopsUser->get('uid'));
					if ($receive_uid === $my_uid) {
						$result = TRUE;
					}
				}
			}
		}

		return $result;
	}

	/**
	 * executeViewInput
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewInput(&$render)
	{
	//	$this->mRoot->mController->executeForward('./index.php?action=RecList');
		$this->mRoot->mController->executeForward(Mileage_Utils::makeUri($this->mAsset->mDirname, 'RecList'));
	}


	/**
	 * executeViewSuccess
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewSuccess(/*** XCube_RenderTarget ***/ &$render)
	{
		// MINUS form UID TODO :-P
	/*	$db =& $this->mRoot->mController->getDB();
		$dirname = $this->mAsset->mDirname;
		$minus_point = $this->mObject->get('point') * -1;
		$sql = sprintf(
				'INSERT INTO `%s` (`point`, `mile_id`, `uid`, `reg_unixtime`) VALUES (%d, %d, %d, %d)',
					$db->prefix($dirname . '_rec'),
					$minus_point,
					intval($this->mObject->get('mile_id')),
					intval($this->mObject->get('uid')),
					time()
			);	*/

	/*	if (! $db->query($sql)) {
			$this->mRoot->mController->executeRedirect(XOOPS_URL . '/', 1, 
				_MD_MILEAGE_ERROR_DBUPDATE_FAILED . '(REMITTANCE ERROR : MINUS POINT)');
		} else {	*/
			$access_uri = $this->mRoot->mContext->mRequest->getRequest('access_uri');
			$r_access_uri = XOOPS_URL . '/';
			if (!empty($access_uri) && Mileage_Utils::checkURI($access_uri)){
				$r_access_uri = $access_uri;
			}
			$this->mRoot->mController->executeForward($r_access_uri);
	//	}
	}

	/**
	 * executeViewError
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewError(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeRedirect(XOOPS_URL . '/', 1, _MD_MILEAGE_ERROR_DBUPDATE_FAILED);
	}

	/**
	 * executeViewCancel
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewCancel(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeForward(XOOPS_URL . '/');
	}

}

